package com.lightsaber.trade.cloth.web.programme;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.lightsaber.trade.core.web.BaseAction;
import com.lightsaber.trade.cloth.dao.programme.BaseProgrammeDao;
import com.lightsaber.trade.cloth.dao.programme.StreamDao;
import com.lightsaber.trade.cloth.entity.programme.BaseProgramme;
import com.lightsaber.trade.cloth.entity.programme.ProgrammePlanProjectionType;
import com.lightsaber.trade.cloth.entity.programme.ProgrammeStatus;
import com.lightsaber.trade.cloth.entity.programme.Stream;
import com.lightsaber.trade.cloth.service.programme.ProgrammePlanProjectionTypeManager;

@Namespace("/programme")
public class ProgrammePlanProjectionTypeAction extends BaseAction {
    private static final long serialVersionUID = 1L;

    private static final Logger log = LoggerFactory
            .getLogger(ProgrammePlanProjectionTypeAction.class);

    // ****************** Autowired *****************START
    @Autowired
    private transient ProgrammePlanProjectionTypeManager programmePlanProjectionTypeManager;

    @Autowired
    private StreamDao streamDao;
    
    @Autowired
    private transient BaseProgrammeDao baseProgrammeDao;
    // ****************** Autowired *****************END

    // ****************** Page parameters *****************START
    private String programmeId;

    private List<ProgrammePlanProjectionType> results;

    private boolean  canEdit;
    
    

    // ****************** Page parameters *****************END

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        BaseProgramme programme = baseProgrammeDao.get(programmeId);
        if(programme.getProgrammeKind().equals("Stream")){
            ProgrammeStatus status = streamDao.get(programmeId).getProgramme().getStatus();
            canEdit = null==status?true:status.isCanEdit();
         }else{
             canEdit = null==programme.getStatus()?true:programme.getStatus().isCanEdit();
         }
        results = programmePlanProjectionTypeManager.findByProgramme(programme);
        return SUCCESS;
    }

    public String save() throws Exception {

        if (results != null && results.size() > 0) {
            for (ProgrammePlanProjectionType projectionType : results) {
                ProgrammePlanProjectionType oldProjectionType = programmePlanProjectionTypeManager
                        .get(projectionType.getId());

                oldProjectionType.setPlanProjectionType(projectionType.getPlanProjectionType());
                oldProjectionType.setSelfDefinedProgressionRate(projectionType
                        .getSelfDefinedProgressionRate());

                programmePlanProjectionTypeManager.save(oldProjectionType);
            }
        }

        addActionMessage(getText(PROCESSED_SUCCESS));
        return list();
    }

    public String getProgrammeId() {
        return programmeId;
    }

    public void setProgrammeId(final String programmeId) {
        this.programmeId = programmeId;
    }

    public List<ProgrammePlanProjectionType> getResults() {
        return results;
    }

    public void setResults(final List<ProgrammePlanProjectionType> results) {
        this.results = results;
    }

    public boolean isCanEdit() {
        return canEdit;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }
}
